/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bdl;

import entities.GCalendar;
import entities.Match;
import entities.Prediction;
import java.util.Date;
import java.util.List;
import javax.persistence.Query;
import javax.persistence.TemporalType;

/**
 *
 * @author olawale
 */
public class UtilityBean {

    private static DataAccess da = DataAccess.getDataAccess();

    public static boolean ping(){
        System.out.println("ping");
        Query query = da.em.createNativeQuery("INSERT INTO visits (LastDate) VALUE(?)");
        query.setParameter(1, new Date(), TemporalType.TIMESTAMP);
        try{
            da.em.getTransaction().begin();
            query.executeUpdate();
            da.em.getTransaction().commit();
            return true;
        }catch(Exception ex){
            System.out.println(ex);
            return false;
        }
    }


    public static GCalendar getCalendar(){
        Query query = da.em.createQuery("Select c from GCalendar c where c.id = 1");
        GCalendar result = (GCalendar) query.getSingleResult();
        return result;
    }

    public static void updateCalendar(GCalendar gcal){
        try{
            da.em.getTransaction().begin();
            da.em.merge(gcal);
            da.em.getTransaction().commit();
        }catch(Exception ex){

        }
    }

//    public static Long insertPrediction(Match match){
//
//        Query query = da.em.createNativeQuery("CALL InsertPrediction(?, ?, ?)");
//
//        query.setParameter(1, match.toString());
//        query.setParameter(2, new Date(), TemporalType.DATE);
//        query.setParameter(3, match.getFixture());
//
////        query.setHint("toplink.pessimistic-lock", "Lock");
//
//        try{
//            Vector ref = (Vector) query.getSingleResult();
//            return (Long)ref.get(0);
//        }catch(Exception ex){
//            System.err.println(ex.toString());
//        }
//        return 0L;
//    }

    public static Prediction insertPrediction(Match match,String score){

        Prediction prediction = new Prediction();
        Match m = MatchBean.createMatch(match);
        prediction.setMatch(m);
        prediction.setActualScore("N/A");
        prediction.setPredictedScore(score);
        prediction.setRating1(-1);
        prediction.setRating2(-1);

        try{
            da.em.getTransaction().begin();
            da.em.persist(prediction);
            da.em.getTransaction().commit();
            return prediction;
        }catch(Exception ex){
            System.err.println(ex.toString());
        }
        return null;
    }

    public static Prediction updatePrediction(Prediction prediction){
        try{
            da.em.getTransaction().begin();
            Prediction p = da.em.merge(prediction);
            da.em.getTransaction().commit();
            return p;
        }catch(Exception ex){
            System.err.println(ex.toString());
        }
        return null;
    }

    public static List<Prediction> getPredictions(){

        Query query = da.em.createNamedQuery("Prediction.findAll");

        try{
            List<Prediction> list = query.getResultList();
            return list;
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
        return null;
    }

    public static List<Prediction> getPredictions(Match match){

        Query query = da.em.createQuery("SELECT p FROM Prediction p where p.match = :m");
        query.setParameter("m", match);

        try{
            List<Prediction> list = query.getResultList();
            return list;
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
        return null;
    }

    public static List<Prediction> getRatedPredictions(){

        Query query = da.em.createQuery("SELECT p FROM Prediction p where p.rating1 > -1 and p.rating2 > -1");

        try{
            List<Prediction> list = query.getResultList();
            return list;
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
        return null;
    }

    public static List<Prediction> getRatedPredictions(Date sdate, Date edate){

        Query query = da.em.createQuery("SELECT p FROM Prediction p WHERE p.match.matchDate " +
                "BETWEEN :sd AND :ed AND p.rating1 > -1 AND p.rating2 > -1");

        query.setParameter("sd", sdate, TemporalType.DATE);
        query.setParameter("ed", edate, TemporalType.DATE);

        try{
            List<Prediction> list = query.getResultList();
            return list;
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
        return null;
    }

    public static List<Match> getPMatches(){

        Query query = da.em.createQuery("SELECT m FROM Match m WHERE m.prediction = 1");

        try{
            List<Match> list = query.getResultList();
            return list;
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
        return null;
    }

    public static void deletePrediction(Prediction prediction){
        try{
            da.em.getTransaction().begin();
            da.em.remove(prediction);
            da.em.getTransaction().commit();
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
    }



    
    

}
